package com.baiytfp.hf.HFDataExchange.action.dao;

import java.sql.Connection;

import org.springframework.stereotype.Repository;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Password_retrieval;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_account;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_info;
import com.baiytfp.hf.HFDataExchange.common.utils.MD5Util;
import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.framework.model.state.YesOrNo;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;

/** 
* @ClassName: PasswordRetrievalDAOImpl 
* @Description: 找回密码dao
* @author 罗赵树
* @date 2017年10月16 下午 04:32:15 by linksoft
*  
*/
@Repository
public class PasswordRetrievalDAOImpl {

	/**
	 * 保存添加一个password_retrieval, 并将此用户的其它key全设为失效
	 * @param password_retrieval
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public long addPasswordRetrieval(Password_retrieval password_retrieval, Connection connection) throws Exception {
		String sql = " update " + TableName.PASSWORD_RETRIEVAL + " set is_valid = ? where user_code = ? and is_valid = ? ";
		HFDBHelper.executeSql(connection, sql, YesOrNo.NO.getKey(), password_retrieval.getUser_code(), YesOrNo.YES.getKey());

		password_retrieval.setId(HFDBHelper.getIDGenerator());
		password_retrieval.setCreate_time(DateUtil.getCurrentDT());
		password_retrieval.setIs_valid(YesOrNo.YES.getKey());
		return HFDBHelper.add(connection, TableName.PASSWORD_RETRIEVAL, password_retrieval);
	}

	/**
	 * 根据ID获取一个效的没被使用过的Password_retrieval
	 * @param id
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public Password_retrieval getPasswordRetrievalById(String id, Connection connection) throws Exception {
		return HFDBHelper.read(connection, TableName.PASSWORD_RETRIEVAL, " where id = ? and is_valid = ? ", Password_retrieval.class
				, id, YesOrNo.YES.getKey());
	}

	/**
	 * 根据key获取一个找回密码
	 * @param id
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public Password_retrieval getPasswordRetrievalByKey(String key, Connection connection) throws Exception {
		return HFDBHelper.read(connection, TableName.PASSWORD_RETRIEVAL, " where retrieval_key = ? ", Password_retrieval.class
				, key);
	}
	
	/**
	 * 删除key, 设置为失效
	 * @param ids
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public long deletePasswordRetrieval(String ids, Connection connection) throws Exception {
		Object[] params =  DBManager.getInStrList(ids);
		String sql = " update " + TableName.PASSWORD_RETRIEVAL + " set is_valid = 0 where id in ( "+ DBManager.getInCharList( params.length) + " )"; 
		return HFDBHelper.executeSql(connection, sql, params);
	}
	
	/**
	 * 更改密码
	 * @param user_code
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public long updatePsd(String user_code, String new_password, Connection connection) throws Exception {
		//update sys_user_account set USER_PASSWORD='123456' where user_code in ('staff2','staff9')
		long result=0;
		String[] usercode=user_code.split(",");
		for(int i=0;usercode.length>i;i++){
			User_account us=HFDBHelper.read(TableName.USER_ACCOUNT, " where USER_CODE=?  ",User_account.class, new Object[]{usercode[i]});
			us.setUser_password(MD5Util.MD5Encode(new_password, "utf-8"));
			result+=HFDBHelper.update(connection,TableName.USER_ACCOUNT, " where USER_CODE=? ", us, new Object[]{usercode[i]});
		}
		return result;
	}

	/**
	 * 根据邮箱获取用户
	 * @param email
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public User_info getStaffByEmail(String email, Connection connection) throws Exception {
		return HFDBHelper.read(connection, TableName.USER_INFO, " where e_mail_addr = ? ", User_info.class, email);
	}

}
